﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Abstract;
using Domain.Entity;
using System.Data;
using System.Text.RegularExpressions;

namespace Domain.Concrete
{
    public class EFCompanyRepository : ICompanyRepository
    {
        public EFDbContext Context { get; set; }

        public EFCompanyRepository()
        {
            Context = EFDbContext.Instance;
        }

        public EFCompanyRepository(EFDbContext context)
        {
            Context = context;
        }

        public IQueryable<Company> Companies
        {
            get { return Context.Companies; }
        }

        public void SaveCompany(Company company)
        {
            if (company.CompanyID == 0)
            {
                Context.Companies.Add(company);
            }
            else
            {
                Context.Entry(company).State = EntityState.Modified;
            }
            Context.SaveChanges();
        }

        public void RemoveCompany(Company company)
        {
            var dbCompany = Companies.SingleOrDefault(x => x.CompanyID == company.CompanyID);

            if(dbCompany != null)
                Context.Companies.Remove(dbCompany);
            Context.SaveChanges();
        }

        public Company FindCompany(int id)
        {
            return Context.Companies.FirstOrDefault(x => x.CompanyID == id);
        }

        public List<Company> SearchCompany(string search = "")
        {
            List<Company> companiesFound = new List<Company>();

            foreach (var c in Context.Companies)
            {
                bool add = true;
                //Searches by name
                if(Regex.IsMatch(c.Name, search))
                //if (c.Name.Equals(search))
                {
                    if (companiesFound.Count > 0)
                    {
                        foreach (var cf in companiesFound)
                        {
                            if (c.Name.Equals(cf.Name))
                            {
                                add = false;
                            }
                        }
                        if (add)
                        {
                            companiesFound.Add(c);
                        }
                    }
                    else
                    {
                        companiesFound.Add(c);
                    }
                }
                //Searches by company type
                if (search.Equals(c.Type))
                {
                    if (companiesFound.Count > 0)
                    {
                        foreach (var cf in companiesFound)
                        {
                            if (c.Name.Equals(cf.Name))
                            {
                                add = false;
                            }
                        }
                        if (add)
                        {
                            companiesFound.Add(c);
                        }
                    }
                    else
                    {
                        companiesFound.Add(c);
                    }
                }

        //        if (search.Equals("Publisher"))
        //        {
        //            Company.CompanyType type = Company.CompanyType.Publisher;
        //            if (type.Equals(c.Type))
        //            {
        //                if (companiesFound.Count > 0)
        //                {
        //                    foreach (var cf in companiesFound)
        //                    {
        //                        if (c.Name.Equals(cf.Name))
        //                        {
        //                            add = false;
        //                        }
        //                    }
        //                    if (add)
        //                    {
        //                        companiesFound.Add(c);
        //                    }
        //                }
        //                else
        //                {
        //                    companiesFound.Add(c);
        //                }
        //            }
        //        }
        //        else if (search.Equals("Developer"))
        //        {
        //            Company.CompanyType type = Company.CompanyType.Developer;
        //            if (type.Equals(c.Type))
        //            {
        //                if (companiesFound.Count > 0)
        //                {
        //                    foreach (var cf in companiesFound)
        //                    {
        //                        if (c.Name.Equals(cf.Name))
        //                        {
        //                            add = false;
        //                        }
        //                    }
        //                    if (add)
        //                    {
        //                        companiesFound.Add(c);
        //                    }
        //                }
        //                else
        //                {
        //                    companiesFound.Add(c);
        //                }
        //            }
        //        }
            }
            return companiesFound;
        }

        
    }
}
